package kalambury.client;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created with IntelliJ IDEA.
 * User: damienix
 * Date: 04.11.12
 * Time: 19:53
 * To change this template use File | Settings | File Templates.
 */

/**
 * klasa implementująca panel do rysowania
 */
public class DrawerPanel extends JComponent {

    private final class DPMenu extends JPopupMenu {
        public DPMenu(){
            JMenuItem clearItem = new JMenuItem("wyczyść");
            clearItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    clearIfMay();
                }
            });
            this.add(clearItem);
        }
    }

    private Image image;
    private Graphics2D graphics2D;
    private int currentX, currentY, oldX, oldY;
    private boolean mayDrawing = false;

    private DPMenu menu = new DPMenu();

    public DrawerPanel() {
        setDoubleBuffered(false);
        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON1){
                    oldX = e.getX();
                    oldY = e.getY();
                    menu.setVisible(false);
                } else {
                    menu.show(DrawerPanel.this,e.getX(),e.getY());
                }
            }
        });

        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                currentX = e.getX();
                currentY = e.getY();

                if (graphics2D != null && mayDrawing)
                    graphics2D.drawLine(oldX, oldY, currentX, currentY);

                repaint();

                oldX = currentX;
                oldY = currentY;
            }
        });
    }

    /**
     * rysuje obraz na ekranie
     * @param g obiekt rysujący
     */
    public void paintComponent(Graphics g) {

        if (image == null) {
            clearIfMay();
        }

        g.drawImage(image, 0, 0, null);
    }

    /**
     * czyści obraz, jeśli może
     */
    public void clearIfMay() {
        clear(mayDrawing);
    }

    /**
     * czyści obraz
     */
    public void clear() {
        clear(true);
    }

    /**
     * czyści obraz, tylko jeśli może to zrobić
     * @param mayClear czy może wyczyścić obraz
     */
    private void clear(boolean mayClear) {
        if(!mayClear)
            return;

        createEmptyImage();
        graphics2D.setPaint(Color.white);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);
        graphics2D.setPaint(Color.black);
        repaint();
    }

    /**
     * pobiera aktualnie przechowywany obraz
     * @return obraz
     */
    public Image getImage(){
        if(image == null){
            createEmptyImage();
        }

        return image;
    }

    /**
     * ustawia obraz do przechowania
     * @param img obraz
     */
    public void setImage(Image img){
        image = img;
        repaint();
    }

    /**
     * ustawia, czy może rysować
     * @param b czy można
     */
    public void setMayDrawing(boolean b){
        mayDrawing = b;
    }

    /**
     * pobira, czy można rysować
     * @return czy można
     */
    public boolean getMayDrawing(){
        return mayDrawing;
    }

    /**
     * tworzy pusty obraz o poprawnym roziarze
     */
    private void createEmptyImage() {
        image = createImage(getSize().width, getSize().height);
        graphics2D = (Graphics2D) image.getGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }
}
